package com.yeyks.common.ajax;

import java.util.HashMap;
import java.util.Map;

/**
 * 通用请求返回结构，用它来包一层返回数据
 *
 * @author Beacon
 * @version $Id: AjaxResponseBody.java, v 0.1 2018年07月30日 17:21 Beacon Exp $
 */
public class AjaxResponseBody<T> {
    private int code;
    private String msg;
    private T body;

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getBody() {
        return body;
    }

    public void setBody(T body) {
        this.body = body;
    }

    public static <T> AjaxResponseBody<T> success(T o) {
        AjaxResponseBody ajaxResponseBody = new AjaxResponseBody();
        ajaxResponseBody.setBody(o);
        ajaxResponseBody.setCode(0);
        ajaxResponseBody.setMsg("success");
        return ajaxResponseBody;
    }

    public static <T> AjaxResponseBody<T> success() {
        AjaxResponseBody<T> ajaxResponseBody = new AjaxResponseBody<>();
        ajaxResponseBody.setCode(0);
        ajaxResponseBody.setMsg("success");
        return ajaxResponseBody;
    }

    public static <T> AjaxResponseBody<T> failed() {
        return failed("");
    }

    public static <T> AjaxResponseBody<T> failed(String errorMsg) {
        AjaxResponseBody<T> ajaxResponseBody = new AjaxResponseBody<>();
        ajaxResponseBody.setCode(-1);
        ajaxResponseBody.setMsg(errorMsg);
        return ajaxResponseBody;
    }

    public static <T> AjaxResponseBody<T> failed(Integer code, String errorMsg) {
        AjaxResponseBody<T> ajaxResponseBody = new AjaxResponseBody<>();
        ajaxResponseBody.setCode(code);
        ajaxResponseBody.setMsg(errorMsg);
        return ajaxResponseBody;
    }

    public static AjaxResponseBody<Map> getMapAjaxResponseBody() {
        AjaxResponseBody<Map> mapAjaxResponseBody = new AjaxResponseBody<>();
        mapAjaxResponseBody.setBody(new HashMap());
        return mapAjaxResponseBody;
    }

    public static AjaxResponseBody create(Boolean b) {
        if (b) {
            return success();
        } else {
            return failed();
        }
    }

}